package com.practice.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.practice.common.result.PageResult;
import com.practice.common.result.Result;
import com.practice.mapper.user.CateMapper;
import com.practice.pojo.admin.dto.PageCateDTO;
import com.practice.pojo.admin.vo.CateVO;
import com.practice.pojo.user.entity.Cate;
import com.practice.service.admin.CateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;


@Service
public class CateServiceImpl implements CateService {
    @Autowired
    private CateMapper cateMapper;

    /**
     * 分类详情
     * @return
     */
    @Override
    public Result selectById(String id) {

        Cate cate = cateMapper.selectById(id);
        CateVO cateVO = new CateVO();
        if (cate!=null){
        BeanUtils.copyProperties(cate,cateVO);}
        return Result.success(cateVO);
    }
    /**
     * 分类分页
     * @return
     */
    @Override
    public Result list(PageCateDTO dto) {
       Integer page = Integer.valueOf(dto.getPage());
        Integer limit = Integer.valueOf(dto.getLimit());
        if (limit==null){
            limit =10;
        }if (page==null){
            page=1;
        }
        //1.创建IPage 分页查询对象，设置分页参数
        IPage<Cate> cates = new Page<>(page,limit);
        //2.设置查询条件 模糊查询dto传入的CateName
        LambdaQueryWrapper<Cate> lqw = new LambdaQueryWrapper<>();
        lqw.like(Cate::getCatename,dto.getCatename());
        cateMapper.selectPage(cates, lqw);
        //3.属性赋值
        List<CateVO> cateVOS = BeanUtil.copyToList(cates.getRecords(), CateVO.class);
        //4.封装的PageResult
        PageResult pageResult = PageResult.builder().count(cates.getTotal())
                .totalPages(cates.getPages())
                .pageSize(cates.getSize())
                .currentPage(cates.getCurrent())
                .data(cateVOS)
                .build();
        return Result.success(pageResult);
    }

    /**
     * 分类删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(String id) {
            cateMapper.deleteById(id);
            return Result.success();
    }

    /**
     * 分类修改
     * @param cate
     * @return
     */
    @Override
    public Result update(Cate cate) {
        cate.setDefaultData(null);
        cateMapper.updateById(cate);
        return Result.success();
    }

    /**
     * 分类添加
     * @return
     */
    @Override
    public Result add(Cate cate) {
        cate.setDefaultData("1");
        cateMapper.insert(cate);
        return Result.success();
    }
    /**
     * 分类列表全部
     * @return
     */
    @Override
    public Result selectAll() {
        List<Cate> cates = cateMapper.selectList(null);
        List<CateVO> cateVOS = BeanUtil.copyToList(cates, CateVO.class);
        return Result.success(cateVOS);
    }

}
